package biz.datalk.industrialland.admin.modules.online.dynform.controller;

import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformHeadDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformField;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformHead;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformServices;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.validated.Delete;
import biz.datalk.industrialland.common.validated.Detail;
import biz.datalk.industrialland.common.validated.Insert;
import biz.datalk.industrialland.common.validated.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author tarofang@163.com
 * @date 2019-12-17
 */
@RestController
@RequestMapping("/dynform")
public class DynformController {

    private final IDynformServices dynformServices;

    @Autowired
    public DynformController(IDynformServices dynformServices) {
        this.dynformServices = dynformServices;
    }

    @RequestMapping(value = "")
    public ModelAndView indexPage() {
        return new ModelAndView("biz/datalk/framework/core/dynform/index");
    }

    @RSAES
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = DynformHead.class, include = "", filter = "createBy,createName,createTime,updateBy,updateName,updateTime,sysOrgCode")
    public UnifyResult list(@RequestBody DynformHeadDTO dynformHeadDTO) {
        return dynformServices.list(dynformHeadDTO);
    }

    @RSAES
    @PostMapping(value = "/addHead", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult addHead(@RequestBody @Validated({Insert.class}) DynformHeadDTO dynformHeadDTO) {
        return dynformServices.addHead(dynformHeadDTO);
    }

    @RSAES
    @PostMapping(value = "/editHead", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult editHead(@RequestBody @Validated({Update.class}) DynformHeadDTO dynformHeadDTO) {
        return dynformServices.editHead(dynformHeadDTO);
    }

    @RSAES
    @PostMapping(value = "/delHead", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult delHead(@RequestBody @Validated({Delete.class}) DynformHeadDTO dynformHeadDTO) {
        return dynformServices.delHead(dynformHeadDTO);
    }

    @RSAES
    @PostMapping(value = "/headDetail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = DynformHead.class, include = "", filter = "createBy,createName,createTime,updateBy,updateName,updateTime,sysOrgCode")
    @Json(type = DynformField.class, include = "", filter = "createBy,createName,createTime,updateBy,updateName,updateTime,sysOrgCode")
    public UnifyResult headDetail(@RequestBody @Validated({Detail.class}) DynformHeadDTO dynformHeadDTO) {
        return dynformServices.headDetail(dynformHeadDTO);
    }

    /** 同步到数据库 */
    @RSAES
    @PostMapping(value = "/doDbSync", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult doDbSync(@RequestBody @Validated({Detail.class}) DynformHeadDTO dynformHeadDTO) {
        return dynformServices.doDbSync(dynformHeadDTO);
    }

    /** 移除 */
    @RSAES
    @PostMapping(value = "/remove", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult remove(@RequestBody @Validated({Detail.class}) DynformHeadDTO dynformHeadDTO) {
        return dynformServices.remove(dynformHeadDTO);
    }

    /** 查询表 */
    @RSAES
    @PostMapping(value = "/queryTables", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult queryTables() {
        return dynformServices.queryTables();
    }

    /** 从数据库导入 */
    @RSAES
    @PostMapping(value = "/importFromDB", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult importFromDB(@RequestBody Map<String, String> params) {
        return dynformServices.importFromDB(params);
    }

}
